package org.easyspring.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.kms.model.v20160120.GetSecretValueRequest;
import com.aliyuncs.kms.model.v20160120.GetSecretValueResponse;
import com.aliyuncs.kms.model.v20160120.ListKeysRequest;
import com.aliyuncs.kms.model.v20160120.ListKeysResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Iterator;
import java.util.List;

public class KmsClient {
    private static String accessKeyId="LTAI5tHuja6fjqrpK38kFKpX";
    private static String regionId="cn-shenzhen";
    private static String accessKeySecret="R3FfNAtbniV06sWqYU9kx7ZRn9geK2";
    private DefaultAcsClient kmsClient;
    private KmsClient(DefaultAcsClient acsClient) {
        this.kmsClient = acsClient;
    }

    /**
     * 创建KmsClient，只需要指定RegionId，SDK自动配置相应地域的公网Endpoint。
     */
    public static KmsClient getClientForPublicEndpoint(String regionId, String accessKeyId, String accessKeySecret) {
        /**
         * Construct an Aliyun Client:
         * Set RegionId, AccessKeyId and AccessKeySecret
         */
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return new KmsClient(client);
    }
    public ListKeysResponse ListKey(int pageNumber, int pageSize) throws ClientException {
        final ListKeysRequest listKeysReq = new ListKeysRequest();

        listKeysReq.setProtocol(ProtocolType.HTTPS);
        listKeysReq.setAcceptFormat(FormatType.JSON);
        listKeysReq.setMethod(MethodType.POST);
        listKeysReq.setPageNumber(pageNumber);
        listKeysReq.setPageSize(pageSize);

        final ListKeysResponse listKeysRes = kmsClient.getAcsResponse(listKeysReq);
        return listKeysRes;
    }

    /*
     *  访问凭据管家前，请在RAM控制台为访问账号添加访问策略（例如：设置密钥管理服务的管理权限AliyunKMSFullAccess）。您也可以添加包含所需的API权限的系统策略或自定义策略。
     * */
    public static DefaultAcsClient getkmsClient() {
        /*
         *  1. 指定凭据管家所在地域。
         *  2. 指定访问KMS所需要的凭证AccessKey ID和AccessKey Secret。
         * */
        IClientProfile profile = DefaultProfile.getProfile(regionId,accessKeyId,accessKeySecret);
        HttpClientConfig clientConfig = HttpClientConfig.getDefault();
        profile.setHttpClientConfig(clientConfig);
        return new DefaultAcsClient(profile);
    }

    public static void GetSecretValueSample(String secret_name,String version_stage) throws ClientException {
        DefaultAcsClient acsClient = getkmsClient();

        GetSecretValueRequest req = new GetSecretValueRequest();
        req.setSecretName(secret_name);
        req.setVersionStage(version_stage);

        GetSecretValueResponse rsp = acsClient.getAcsResponse(req);
        System.out.printf("GetSecretValue  data: %s \n",rsp.getSecretData());
    }

    public static void main(String[] args) {
        try {
            GetSecretValueSample("shardingsphere.encrypt.dkms","");
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
